/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 9/06/2004
 *
 */

package au.com.lastweekend.jim.io.exif;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Logger;

import au.com.lastweekend.jim.io.metadata.Directory;
import au.com.lastweekend.jim.util.LoggingOutputStream;

/**
 * @author grant@lastweekend.com.au
 * @version $Id: TiffDirectoryWriter.java,v 1.3 2006/01/28 05:05:47 ggardner Exp $
 * 
 */
public class TiffDirectoryWriter extends AbstractTiffTagWriter {

    private static final int START_OFFSET = 8;
    private static final Logger LOG = Logger.getLogger(TiffDirectoryWriter.class);

    /**
     * @param directory
     */
    public TiffDirectoryWriter(TiffDirectory directory) {

        this(directory, ExifTransform.NO_TRANSFORM);

    }

    /**
     * @param directory
     * @param exifTransform
     */
    public TiffDirectoryWriter(TiffDirectory directory, ExifTransform exifTransform) {

        super(directory, exifTransform);
        setDataFormat(TiffTagFormat.getInstance(directory.isMotorolaByteOrder()));
        setStartOffset(START_OFFSET);
    }

    @Override
    public int getDirectoryLength() {

        return START_OFFSET + super.getDirectoryLength();
    }

    @Override
    public void writeDirectory(OutputStream output) throws IOException {

        if (LOG.isDebugEnabled()) {
            if (output instanceof LoggingOutputStream) {
                LoggingOutputStream loggingOutputStream = ((LoggingOutputStream) output);
                loggingOutputStream.pushOffset();
                LOG.debug("Starting Tiff directory at: " + loggingOutputStream.getStreamPosition());

            } else {
                LOG.debug("Starting Tiff directory");
            }
        }
        writeHeader(output);
        writeTags(output);
        if (LOG.isDebugEnabled()) {
            if (output instanceof LoggingOutputStream) {
                LoggingOutputStream loggingOutputStream = ((LoggingOutputStream) output);
                LOG.debug("Ending Tiff directory at: " + loggingOutputStream.getStreamPosition());
                loggingOutputStream.popOffset();

            } else {
                LOG.debug("Ending Tiff directory");

            }
        }

    }

    @Override
    protected DirectoryWriter getLinkedDirectoryWriter(int startOffset) {

        Directory linkedDirectory = getDirectory().getLinkedDirectory();
        if (linkedDirectory != null && linkedDirectory instanceof ThumbnailTiffDirectory) {
            ThumbnailTiffDirectoryWriter writer = new ThumbnailTiffDirectoryWriter((ThumbnailTiffDirectory) linkedDirectory, this
                    .getExifTransform());
            writer.setDataFormat(getDataFormat());
            writer.setStartOffset(startOffset);
            return writer;

        } else {
            return super.getLinkedDirectoryWriter(startOffset);
        }
    }

    @Override
    protected DirectoryWriter getDirectoryWriter(int tagType, Directory directory, int startOffset) {

        switch (tagType) {
            case TiffDirectory.TAG_EXIF_OFFSET :
                ExifDirectoryWriter exifDirectoryWriter = new ExifDirectoryWriter((ExifDirectory) directory, this
                        .getExifTransform());
                exifDirectoryWriter.setDataFormat(getDataFormat());
                exifDirectoryWriter.setStartOffset(startOffset);
                return exifDirectoryWriter;

            case TiffDirectory.TAG_GPS_INFO_OFFSET :
                GPSDirectoryWriter gpsDirectoryWriter = new GPSDirectoryWriter((GPSDirectory) directory, this.getExifTransform());
                gpsDirectoryWriter.setDataFormat(getDataFormat());
                gpsDirectoryWriter.setStartOffset(startOffset);
                return gpsDirectoryWriter;
            default :
                return super.getDirectoryWriter(tagType, directory, startOffset);
        }

    }

    @Override
    protected Object getTransformedTagValue(int tagType) {

        if (getExifTransform().isXYTransposed()) {
            switch (tagType) {
                case ExifDirectory.TAG_X_RESOLUTION :
                    return getDirectory().getObject(ExifDirectory.TAG_Y_RESOLUTION);
                case ExifDirectory.TAG_Y_RESOLUTION :
                    return getDirectory().getObject(ExifDirectory.TAG_X_RESOLUTION);
                default :
                    return super.getTransformedTagValue(tagType);
            }

        }
        return super.getTransformedTagValue(tagType);
    }

    /*
     * @see au.com.lastweekend.jim.io.exif.DirectoryWriter#getComponentCount()
     */
    public int getComponentCount() {

        throw new UnsupportedOperationException("Not expecting Tiff to be a sub directory");
    }
}